package cn.ad.server.common.utils;

import java.util.Collection;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.joda.time.DateTime;

import com.google.common.collect.Lists;

public class BeanUtils {

	private static DozerBeanMapper dozer = new DozerBeanMapper();

	public static <T> T map(Object source, Class<T> destinationClass) {
		if (source == null)
			return null;
		return dozer.map(source, destinationClass);
	}

	/**
	 * 基于Dozer转换Collection中对象的类型.
	 */
	public static <T> List<T> mapList(Collection<?> sourceList, Class<T> destinationClass) {
		if (sourceList == null || sourceList.isEmpty())
			return null;
		List<T> destinationList = Lists.newArrayList();
		for (Object sourceObject : sourceList) {
			T destinationObject = dozer.map(sourceObject, destinationClass);
			destinationList.add(destinationObject);
		}
		return destinationList;
	}

	/**
	 * Collection中对象的类型Integer进行clone.
	 */
	public static List<Integer> mapIntegerList(Collection<Integer> sourceList) {
		if (sourceList == null || sourceList.isEmpty())
			return null;
		List<Integer> destinationList = Lists.newArrayList();
		for (Integer sourceObject : sourceList) {
			destinationList.add(new Integer(sourceObject.intValue()));
		}
		return destinationList;
	}
	
	
	/**
	 * Collection中对象的类型Long进行clone.
	 */
	public static List<Long> mapLongList(Collection<Long> sourceList) {
		if (sourceList == null || sourceList.isEmpty())
			return null;
		List<Long> destinationList = Lists.newArrayList();
		for (Long sourceObject : sourceList) {
			destinationList.add(new Long(sourceObject.intValue()));
		}
		return destinationList;
	}
	

	/**
	 * 基于Dozer将对象A的值拷贝到对象B中.
	 */
	public static void copy(Object source, Object destinationObject) {
		if (source == null)
			return;
		dozer.map(source, destinationObject);
	}

	public static int aa(DateTime updateTime, DateTime now) {
		int total = 0;
		// 如果time为0,代表这个策略没有产生量的减少。要从当天0点开始，把量给回追回来
		// if (time == 0) {
		// // 从当天的0点开始追量，初始间隔设为1
		// total = 1;
		// time = new DateTime(now.getYear(), now.getMonthOfYear(),
		// now.getDayOfMonth(), 0, 0, 0).getMillis();
		// }
		int diffHour = now.getHourOfDay() - updateTime.getHourOfDay();
		total += diffHour * 2;
		if (30 - updateTime.getMinuteOfHour() > 0 && 30 - now.getMinuteOfHour() <= 0)
			total += 1;
		else if (30 - updateTime.getMinuteOfHour() < 0 && 30 - now.getMinuteOfHour() > 0)
			total -= 1;
		return total;
	}

	public static int getTotalBucket(long s, long e, int timeBucket) {
		DateTime now = new DateTime(e);
		long start = new DateTime(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), 0, 0, 0).getMillis();
		int total = 0;
		if (s == 0) {
			s = start;
		}
		if (s == start)
			total += 1;
		long a = now.getMillis() - start;
		long b = s - start;
		total += a / (timeBucket * 60 * 1000) - b / (timeBucket * 60 * 1000);
		return total;
	}

//	public static void main(String[] args) throws Exception {
//		List<String> a = new ArrayList<String>();
//		a.add("1");
//		a.add("2");
//		a.add("3");
//		p(10, a.toArray(new String[a.size()]));
//	}
//	
//	
//	static void p(int i, String... strings) {
//		System.out.println(i);
//		for(String s : strings) {
//			System.out.println(s);
//		}
//	}
	
	public static String convertIosUrl(String url, int os) {
		if(!url.startsWith("http")) {
			url = "http://";
		}
		if(os == 2) {
			url = url.replace("http:", "https:");
		}
		return url;
	}
	
}
